package com.example.paper1024.controller;

import com.example.paper1024.entity.User;
import com.example.paper1024.imply.UserImply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserImply userImply;

    // 登录接口
    @PostMapping("/login")
    public boolean login(@RequestParam String username, @RequestParam String password) {
        User user = userImply.queryUserByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return true;
        }
        return false;
    }

    // 注册接口
    @PostMapping("/register")
    public boolean register(@RequestParam String username, @RequestParam String password) {
        User existingUser = userImply.queryUserByUsername(username);
        if (existingUser != null) {
            return false; // 用户已存在
        }
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setPassword(password);
        return userImply.addUser(newUser);
    }
}
